package com.jiantong.work.impl.manager;

import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.model.User;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.DocumentsType;
import com.jiantong.work.impl.dao.OtherPurchaseRecordDao;
import com.jiantong.work.impl.dao.ReportHandleResumeDao;
import com.jiantong.work.model.OtherPurchaseRecord;
import com.jiantong.work.model.ReportHandleResume;
import com.jiantong.work.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by niufei on 2017/4/10.
 *
 */
@Component
public class OtherPurchaseRecordManager {

    private final OtherPurchaseRecordDao otherPurchaseRecordDao;

    private final ReportHandleResumeDao reportHandleResumeDao;

    @Autowired
    public OtherPurchaseRecordManager(OtherPurchaseRecordDao otherPurchaseRecordDao, ReportHandleResumeDao reportHandleResumeDao) {
        this.otherPurchaseRecordDao = otherPurchaseRecordDao;
        this.reportHandleResumeDao = reportHandleResumeDao;
    }

    /**
     * 新建单据
     * @param otherPurchaseRecord 其他购买
     * @param toStatus 到状态
     * @return Map<String , Object>
     */
    @Transactional
    public Map<String , Object> create(OtherPurchaseRecord otherPurchaseRecord , Integer toStatus){
        Integer fromStatus = otherPurchaseRecord.getStatus();
        ReportHandleResume reportHandleResume;
        Map<String , Object> map = Maps.newHashMap();

        LoginUser loginUser = UserUtil.getCurrentUser();
        String prefix = "CQT";
        prefix = Utils.createPrefix(prefix,loginUser);
        String number = Utils.create(prefix,otherPurchaseRecordDao.findCurrentMaxNumber(prefix));
        otherPurchaseRecord.setNumber(number);
        otherPurchaseRecord.setDeleteFlag(DocumentsDeleteFlag.NORMAL.value());
        if(toStatus != null && !fromStatus.equals(toStatus)){
            otherPurchaseRecord.setStatus(toStatus);
            otherPurchaseRecordDao.create(otherPurchaseRecord);
            Long id = otherPurchaseRecord.getId();
            map.put("otherPurchaseId",id);

            reportHandleResume =  new ReportHandleResume();

            reportHandleResume.setReportId(id);
            reportHandleResume.setReportNumber(otherPurchaseRecord.getNumber());
            reportHandleResume.setUserId(loginUser.getId());
            reportHandleResume.setUserName(loginUser.getName());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResume.setType(DocumentsType.OTHER_PURCHASE_RECORD.value());
            reportHandleResumeDao.create(reportHandleResume);

            map.put("reportHandleResumeId",reportHandleResume.getId());
        }else {
            otherPurchaseRecordDao.create(otherPurchaseRecord);
            Long id = otherPurchaseRecord.getId();
            map.put("otherPurchaseId",id);
        }

        return map;
    }


    /**
     * 更新单据
     * @param otherPurchaseRecord 其他购买
     * @param toStatus 到状态
     * @return Map<String , Object>
     */
    @Transactional
    public Map<String , Object> update(OtherPurchaseRecord otherPurchaseRecord , Integer toStatus){
        Integer fromStatus = otherPurchaseRecord.getStatus();
        ReportHandleResume reportHandleResume;
        Map<String , Object> map = Maps.newHashMap();
        if(toStatus != null && !fromStatus.equals(toStatus)){
            otherPurchaseRecord.setStatus(toStatus);
            map.put("otherRecordId",otherPurchaseRecordDao.update(otherPurchaseRecord));

            reportHandleResume =  new ReportHandleResume();

            reportHandleResume.setReportId(otherPurchaseRecord.getId());
            reportHandleResume.setReportNumber(otherPurchaseRecord.getNumber());
            reportHandleResume.setUserId(UserUtil.getCurrentUser().getId());
            reportHandleResume.setUserName(UserUtil.getCurrentUser().getName());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResume.setType(DocumentsType.OTHER_PURCHASE_RECORD.value());
            reportHandleResumeDao.create(reportHandleResume);

            map.put("reportHandleResumeId",reportHandleResume.getId());
        }else {
            map.put("otherRecordId",otherPurchaseRecordDao.update(otherPurchaseRecord));
        }
        return map;
    }


    public Boolean update(OtherPurchaseRecord otherPurchaseRecord) {
        return otherPurchaseRecordDao.update(otherPurchaseRecord);
    }
}
